<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<!-- <div class="right-panel-search" style="margin-left: 10px;">
					<el-select style="width: 100px;" v-model="search.type" clearable
						placeholder="类型" filterable>
						<el-option v-for="(item, key) in types" :key="key" :label="item" :value="key" />
					</el-select>
					<el-select style="width: 100px;" v-model="search.use_type" clearable
						placeholder="适用类型" filterable>
						<el-option v-for="(item, key) in use_types" :key="key" :label="item" :value="key" />
					</el-select>
					<el-select style="width: 100px;" v-model="search.status" clearable
						placeholder="状态" filterable>
						<el-option v-for="(item, key) in statuss" :key="key" :label="item" :value="key" />
					</el-select>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div> -->
			</div>
			<div class="right-panel">
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<el-table-column label="ID" prop="id" width="50"></el-table-column>
				<el-table-column label="会员卡名称" prop="coupon_name" width="150"></el-table-column>
				<el-table-column label="类型" prop="type" width="150">
					<template #default="scope">
						<p >{{types[scope.row.type]}}</p>
					</template>
				</el-table-column>
				<el-table-column label="原价" prop="price" width="150"></el-table-column>
				<el-table-column label="售价" prop="sell_price" width="150"></el-table-column>
				<el-table-column label="备注" prop="remark" width="150"></el-table-column>
				<el-table-column label="排序" prop="sort" width="100"></el-table-column>
				<el-table-column label="状态" prop="status" width="80">
					<template #default="scope">
						<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" :inactive-value="1"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="create_time" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>


</template>

<script>
	import saveDialog from './components/cardSave.vue'

	export default {
		name: 'configureList',
		components: {
			saveDialog,
		},
		data() {
			return {
				dialog:{
					save: false,
				},
				search: {
					type: '',
					receive_type: '1',
					use_type: '',
					status: '1',
				},
				list: {
					apiObj: this.$API.marketing.MemberCard.list
				},
				// 类型：1-月卡会员；2-年卡会员；3-永久会员   4-日卡会员 5-周卡会员 6-季卡会员
				types: {'1': '月卡', '2': '年卡', '3': '永久', '4': '日卡', '5': '周卡', '6': '季卡'},
				statuss: {'0': '启用', '1': '禁用'}
			}
		},
		mounted() {

		},
		methods: {
			//表格内开关
			async changeSwitch(val, row){
				row.status = row.status == '1'?'0':'1'
				let data = JSON.parse(JSON.stringify(row))
				delete data.create_time
				row.$switch_status = true;
				var res = await this.$API.marketing.MemberCard.changeStatus.post({id: data.id, status: val});
				if(res.code == 200){
					delete row.$switch_status;
					row.status = val;
					this.$message.success("操作成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//标签切换
			tabChange(name) {
				this.search.receive_type = name
				this.$refs.table.reload({
					...this.search
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.marketing.MemberCard.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch() {
				// console.log(112, this.search)
				let search = JSON.parse(JSON.stringify(this.search))
				this.$refs.table.upData({
					...search
				})
			},
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},

			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
